package com.example.wordmemory;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wordmemory.entity.Word;
import com.example.wordmemory.utils.WordDBHelper;
import com.example.wordmemory.utils.WordAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class WordManagement extends AppCompatActivity {
    private WordDBHelper dbHelper;
    private WordAdapter adapter;
    private ArrayList<Word> wordList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_management);

        dbHelper = new WordDBHelper(this);

        RecyclerView rv = findViewById(R.id.rv_word_list);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WordAdapter(wordList, new WordAdapter.OnWordActionListener() {
            @Override
            public void onEdit(Word word) {
                showWordDialog(word);
            }
            @Override
            public void onDelete(Word word) {
                dbHelper.deleteWord(word.id);
                loadWords();
            }
        });
        rv.setAdapter(adapter);

        findViewById(R.id.btn_add_word).setOnClickListener(v -> showWordDialog(null));

        // 添加返回按钮点击事件
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());

        loadWords();
    }

    private void loadWords() {
        wordList.clear();
        var cursor = dbHelper.getAllWords();
        while (cursor.moveToNext()) {
            Word w = new Word();
            w.id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
            w.word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
            w.meaning = cursor.getString(cursor.getColumnIndexOrThrow("meaning"));
            w.example = cursor.getString(cursor.getColumnIndexOrThrow("example"));
            w.modifyTime = cursor.getString(cursor.getColumnIndexOrThrow("modify_time"));
            wordList.add(w);
        }
        cursor.close();
        adapter.notifyDataSetChanged();
    }

    // 新增/编辑弹窗
    private void showWordDialog(Word editWord) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_word, null);
        EditText etWord = view.findViewById(R.id.et_word);
        EditText etMeaning = view.findViewById(R.id.et_meaning);
        EditText etExample = view.findViewById(R.id.et_example);

        if (editWord != null) {
            etWord.setText(editWord.word);
            etMeaning.setText(editWord.meaning);
            etExample.setText(editWord.example);
        }

        new AlertDialog.Builder(this)
            .setTitle(editWord == null ? "添加单词" : "编辑单词")
            .setView(view)
            .setPositiveButton("保存", (d, w) -> {
                String word = etWord.getText().toString().trim();
                String meaning = etMeaning.getText().toString().trim();
                String example = etExample.getText().toString().trim();
                if (TextUtils.isEmpty(word) || TextUtils.isEmpty(meaning)) {
                    Toast.makeText(this, "单词和释义不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date());
                ContentValues values = new ContentValues();
                values.put("word", word);
                values.put("meaning", meaning);
                values.put("example", example);
                values.put("modify_time", now);
                if (editWord == null) {
                    dbHelper.addWord(word, meaning, example, now);
                } else {
                    dbHelper.updateWord(editWord.id, values);
                }
                loadWords();
            })
            .setNegativeButton("取消", null)
            .show();
    }

    @Override
    protected void onDestroy() {
        dbHelper.close();
        super.onDestroy();
    }
}